package com.kuaimacode.kframework.admin.services.category;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.agent.SysAgentCategoryCfgMapper;
import com.kuaimacode.kframework.mybatis.models.agent.SysAgentCategoryCfg;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 * @date 2019/03/18
 */
@Service
public class  CategoryCfgService extends ServiceImpl<SysAgentCategoryCfgMapper, SysAgentCategoryCfg> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<SysAgentCategoryCfg> findAll(int pageNum, int pageSize,SysAgentCategoryCfg categoryCfg) {
    		PageHelper.startPage(pageNum, pageSize);
    		QueryWrapper queryWrapper = new QueryWrapper();
    		queryWrapper.eq("agent_id", "000000");
    		queryWrapper.eq("delete_state", "0");
			Optional.ofNullable(categoryCfg.getType()).ifPresent(type->queryWrapper.eq("type",type));
			Optional.ofNullable(categoryCfg.getCategory()).ifPresent(category->queryWrapper.like("category",category));

    		List<SysAgentCategoryCfg> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<SysAgentCategoryCfg>(datas);
    	}

    	public int deleteCategory(String id){
    		//删除总站
			SysAgentCategoryCfg sysAgentCategoryCfg = baseMapper.selectById(id);
			UpdateWrapper updateWrapper = new UpdateWrapper();
			updateWrapper.eq("delete_state","0");
			updateWrapper.eq("id",id);
			sysAgentCategoryCfg.setDeleteState(1);

    		int upCount = baseMapper.update(sysAgentCategoryCfg,updateWrapper);
    		//删除已经设置的分站
			baseMapper.deleteAllAgentCategory(id);

    		return upCount;

		}

    	public int saveDefaultCategory(SysAgentCategoryCfg categoryCfg){
    		//系统默认 品类
			categoryCfg.setAgentId("000000");
    		categoryCfg.setCreateAt(new Date());

    		return baseMapper.insert(categoryCfg);


		}


}

